
import axios from 'axios'
import { API_URL } from '../constants'

const Request = axios.create({
  baseURL: API_URL,
  headers: {
    'Content-Type': 'application/json'
  }
})

/**
 * 请求拦截器
 * 这里可以将请求的数据进行处理再发送到服务器
 */
Request.interceptors.request.use(
  config => {
    // const tokenType = store.getState().userReducer.token_type
    // const token = store.getState().userReducer.access_token

    // 处理非get请求传参问题
    if (config.method !== 'get') {
      config.data = config.params
    }
    // if (token) {
    //   config.headers['Authorization'] = tokenType + ' ' + token
    // }
    console.log('request config->', config)
    return config
  },
  error => {
    console.log(error)
    Promise.reject(error)
  }
)

/**
 * 响应拦截器
 * 将响应拦截下来，对响应先进行处理
 */
Request.interceptors.response.use(
  response => {
    console.log('response-->', response)
    if (response && response.status === 200) {
      return response.data
    } else if (response && response.status === 401) {
      return 'Token Invalid'
    }
  },
  error => {
    console.log('err', error)
    return Promise.reject(error)
  }
)

export default Request
